using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Resources;
using System.IO;
using System.Xml;
using System.Reflection;

namespace SilverlightSnoop
{
    public class Xap
    {
        public string Name { get; set; }
        public string Uri { get; set; }
        public string AssemblyManifest { get; set; }

        public Stream LoadedXAP { get; private set; }

        public List<XapAssembly> Assemblies { get; set; }

        public List<Assembly> LoadedAssemblies { get; set; }

        /// <summary>
        /// Initializes a new instance of the Xap structure.
        /// </summary>
        public Xap()
        {
            Assemblies = new List<XapAssembly>();
            LoadedAssemblies = new List<Assembly>();

            foreach (var item in Deployment.Current.Parts)
            {
                var resStream = Application.GetResourceStream(new Uri(item.Source, UriKind.RelativeOrAbsolute));
                if(resStream == null)
                    continue;

                var asm = new AssemblyPart().Load(resStream.Stream);

                if(asm == null)
                    continue;

                LoadedAssemblies.Add(asm);
            }

            foreach (var item in LoadedAssemblies)
            {
                Assemblies.Add(new XapAssembly(item, this));
            }

            this.Name = "Current Application";
            //Name = uri;
            //Uri = uri;
            //LoadedXAP = result;
            //StreamResourceInfo resourceInfo = new StreamResourceInfo(result, null);
            //StreamResourceInfo manifestInfo = Application.GetResourceStream(resourceInfo, new Uri("AppManifest.xaml", UriKind.Relative));

            //StreamReader manifestReader = new StreamReader(manifestInfo.Stream);
            //String manifest = manifestReader.ReadToEnd();
            //manifestReader.Close();
            //manifestReader.Dispose();

            //XmlReader reader = XmlReader.Create(new StringReader(manifest));

            //List<Assembly> asms = new List<Assembly>();

            //while (reader.Read())
            //{
            //    if (reader.IsStartElement("AssemblyPart"))
            //    {
            //        reader.MoveToAttribute("x:Name");
            //        reader.ReadAttributeValue();

            //        String name = reader.Value;

            //        reader.MoveToAttribute("Source");
            //        reader.ReadAttributeValue();

            //        String assemblyName = reader.Value;

            //        Deployment.Current.Parts.Add(new AssemblyPart() { Source = assemblyName });

            //        var binStream = new StreamResourceInfo(result, "application/binary");
            //        var asmStream = Application.GetResourceStream(binStream, new Uri(assemblyName, UriKind.RelativeOrAbsolute));

            //        AssemblyPart part = new AssemblyPart();
            //        Assembly asm = part.Load(asmStream.Stream);

            //        Application.Current.Resources.Add(assemblyName, asm);

            //        //System.Diagnostics.Debug.WriteLine("Found: {0} at {1}", name, assemblyName);
            //        if (asm != null)
            //        {

            //            try
            //            {
            //                var satlAsmInvariant = asm.GetSatelliteAssembly(System.Globalization.CultureInfo.InvariantCulture);
            //            }
            //            catch (Exception ex)
            //            {
                            
            //            }

            //            try
            //            {
            //                var satlAsmCurrentUI = asm.GetSatelliteAssembly(System.Globalization.CultureInfo.CurrentUICulture);
            //            }
            //            catch (Exception ex)
            //            {
                            
            //            }

            //            try
            //            {
            //                var satlAsmCurrent = asm.GetSatelliteAssembly(System.Globalization.CultureInfo.CurrentCulture);
            //            }
            //            catch (Exception ex)
            //            {
                            
            //            }

            //            asms.Add(asm);
            //        }
            //    }
            //}

            //foreach (var asm in asms)
            //{
            //    this.Assemblies.Add(new XapAssembly(asm, this));
            //}

        }

    }
}
